package com.szholly.data.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.szholly.data.general.*;

/**
 * 类功能说明 定义Ado的oledb类型数据读取对象
 */
public class DbDataReader<T extends RowBase> implements IDataReader {

	private ResultSet rowSet;
	private RowBaseMapper<T> rowMapper;
	private RowBase row = null;

	public DbDataReader(Class<T> c, ResultSet rs) {
		if (rs == null)
			throw new RuntimeException("游标为空");
		this.rowSet = rs;
		this.rowMapper = new RowBaseMapper<T>(c);
	}

	/**
	 * 获取当前行
	 */
	@SuppressWarnings("unchecked")
	public T getRow() {
		if (row != null) {
			return (T) row;
		}
		try {
			row = rowMapper.mapRow(rowSet, rowSet.getRow());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (T) row;
	}

	/**
	 * 将读取器前进到结果集中的下一个记录
	 */
	public boolean read() {
		row = null;
		try {
			return rowSet.next();
		} catch (SQLException e) {
			return false;
		}
	}
}